//
//  DDShareManager.m
//  DingDongZP
//
//  Created by Jyh on 16/5/23.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#define MAX_WX_IMAGE_SIZE 32*1024
#define WY_IMAGE_COMPRESSION_QUALITY 0.4

#import "DDShareManager.h"
#import "SVProgressHUD.h"

@implementation DDShareManager

+ (DDShareManager*)shareInstance
{
    static DDShareManager *share = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[DDShareManager alloc] init];
    });
    
    return share;
}

- (BOOL)shareToWeiXinWithScene:(int)scene title:(NSString *)title description:(NSString *)description webpageUrl:(NSString *)webpageUrl image:(UIImage*)image isVideo:(BOOL)isVideo
{
    if (!([WXApi isWXAppInstalled])) {
        
        if (scene == WXSceneTimeline) {
            
            [[LDShowHUB shareInstance] showErr:@"朋友圈分享失败"];
        } else {
            
            [[LDShowHUB shareInstance] showErr:@"微信分享失败"];
        }
        
        return NO;
    }
    
    WXMediaMessage *msg = [WXMediaMessage message];
    msg.title = title;
    msg.description = description;
    
    if (msg.title.length > 512) {
        msg.title = [msg.title substringToIndex:512];
    }
    if (msg.description.length>1024) {
        msg.description = [msg.description substringToIndex:1024];
    }
    
    if (scene == WXSceneTimeline) {
        msg.description = msg.title;
    }
    
    if (isVideo) {
        //分享视频
        WXVideoObject *video = [WXVideoObject object];
        video.videoUrl = webpageUrl;
        video.videoLowBandUrl = video.videoUrl;
        msg.mediaObject = video;
    }else{
        
        WXWebpageObject *ext = [WXWebpageObject object];
        ext.webpageUrl = webpageUrl;
        msg.mediaObject = ext;
    }
    
    NSData *imgData = nil;
    
    if (!image) {
        image = [UIImage imageNamed:@"launch_logo"];
    }
    if (image) {
        imgData = UIImageJPEGRepresentation(image, WY_IMAGE_COMPRESSION_QUALITY);
        if (imgData.length > MAX_WX_IMAGE_SIZE) {//try again
            imgData = UIImageJPEGRepresentation(image, WY_IMAGE_COMPRESSION_QUALITY/2);
        }
        
    }
    if (imgData && imgData.length < MAX_WX_IMAGE_SIZE) {
        [msg setThumbData:imgData];
    }else{
        
    }
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = msg;
    req.scene = scene;
    BOOL ret = [WXApi sendReq:req];
//    NSLog(@"shareToWX send ret:%d", ret);
    return ret;

}

- (void)shareToWeibo:(DDWeiboShareResultBlock)result title:(NSString *)title description:(NSString *)description webpageUrl:(NSString *)webpageUrl image:(UIImage*)image VC:(id)VC isVideo:(BOOL)isVideo
{
    
    if ([WeiboSDK isCanSSOInWeiboApp] == false) {

        [[LDShowHUB shareInstance] showErr:@"微博分享失败"];
        return;
    }
    
    self.shareBlock = result;
    
    WBImageObject *msg = [WBImageObject object];
    NSData *imgData = nil;
    if (!image) {
        
        image = [UIImage imageNamed:@"launch_logo"];
    }
    if (image) {
        
        imgData = UIImageJPEGRepresentation(image, WY_IMAGE_COMPRESSION_QUALITY);
        if (imgData.length > MAX_WX_IMAGE_SIZE) {//try again
            imgData = UIImageJPEGRepresentation(image, WY_IMAGE_COMPRESSION_QUALITY/2);
        }
    }
    if (imgData && imgData.length < MAX_WX_IMAGE_SIZE) {
        msg.imageData = imgData;
    }else{
        
    }
    
    WBMessageObject *sendMsg = [WBMessageObject message];
    sendMsg.imageObject = msg;
  
    NSString* shareTitle = [NSString stringWithFormat:@"%@  %@",title,@"(分享自@叮咚快聘)"];
    sendMsg.text = shareTitle;
    
    //不能SSO分享
    //    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest request];
    //    request.message = sendMsg;
    
    //SSO分享
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
//    authRequest.redirectURI = Sina_RedirectURL;
    authRequest.scope = @"all";
    NSString *vcStr = NSStringFromClass([VC class]);
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:sendMsg authInfo:authRequest access_token:nil];
    request.userInfo = @{@"ShareMessageFrom":vcStr};
    
    BOOL ret = [WeiboSDK sendRequest:request];
    NSLog(@"shareToWb send ret:%d", ret);

}

@end
